//
//  SaTime.m
//  saas
//
//  Created by fangyibao on 2020/6/8.
//  Copyright © 2020 fangmao. All rights reserved.
//

#import "SaTime.h"

@implementation SaTime

// 时间戳转时间
+(NSString *)transToTime:(NSString *)timesp{
// 格式化时间
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy年MM月dd日"];
    
    // 毫秒值转换为秒
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timesp doubleValue] / 1000.0];
    NSString * dateString = [formatter stringFromDate:date];
    
    
    return dateString;
}

+(NSString *)transStringToTime:(NSString *)timesp{
// 格式化时间
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    
    // 毫秒值转换为秒
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timesp doubleValue] / 1000.0];
    NSString * dateString = [formatter stringFromDate:date];
    
    
    return dateString;
}



+(NSString *)transFormTime:(NSString *)time{
    NSDateFormatter * dateformatter = [[NSDateFormatter alloc]init];
    [dateformatter setTimeZone:[NSTimeZone localTimeZone]];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate * date = [dateformatter dateFromString:time];
    NSString * timeSp = [NSString stringWithFormat:@"%ld",(long)[date timeIntervalSince1970]];


    NSTimeInterval times = [timeSp doubleValue];
    NSDate * detailDate = [NSDate dateWithTimeIntervalSince1970:times];
    NSDateFormatter * datef = [[NSDateFormatter alloc]init];
    [datef setTimeZone:[NSTimeZone localTimeZone]];
    [datef setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
    NSString * cus = [datef stringFromDate:detailDate];


    return cus;
    
}


+(NSString *)transFormTime:(NSString *)startime endStr:(NSString *)end{
    
    NSDateFormatter * dateformatter = [[NSDateFormatter alloc]init];
    [dateformatter setTimeZone:[NSTimeZone localTimeZone]];
    [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate * date = [dateformatter dateFromString:startime];
    NSString * timeSp = [NSString stringWithFormat:@"%ld",(long)[date timeIntervalSince1970]];

    NSTimeInterval times = [timeSp doubleValue];
    NSDate * detailDate = [NSDate dateWithTimeIntervalSince1970:times];
    
    
    NSDateFormatter * dateformatter1 = [[NSDateFormatter alloc]init];
    [dateformatter1 setTimeZone:[NSTimeZone localTimeZone]];
    [dateformatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate * date1 = [dateformatter dateFromString:end];
    NSString * timeSp1 = [NSString stringWithFormat:@"%ld",(long)[date1 timeIntervalSince1970]];

    NSTimeInterval times1 = [timeSp1 doubleValue];
    NSDate * detailDate1 = [NSDate dateWithTimeIntervalSince1970:times1];
    
    NSTimeInterval timeInt = [detailDate1 timeIntervalSinceDate:detailDate];
    
    // 初始化日期组件为零
    NSDateComponents *components = [[NSDateComponents alloc] init];
    
    // 计算小时、分钟和秒
    int seconds = timeInt;
    components.second = seconds % 60;
    int minutes = seconds / 60;
    components.minute = minutes % 60;
    components.hour = minutes / 60;
    
    NSString * hourTime;
    NSString * minuteTime;
    NSString * second;
    
    if (components.hour > 0){
        hourTime = [NSString stringWithFormat:@"%@小时",@(components.hour)];
    }
    if (components.minute > 0){
        minuteTime = [NSString stringWithFormat:@"%@分钟",@(components.minute)];
    }
    if (components.second > 0){
        second = [NSString stringWithFormat:@"%@秒",@(components.second)];
    }
    
    NSString * time = [NSString stringWithFormat:@"%@%@%@",hourTime?hourTime:@"",minuteTime?minuteTime:@"",second?second:@""];
    return time;
    
}


- (NSDateComponents *)hmsFromTimeInterval:(NSTimeInterval)timeInterval {
    // 初始化日期组件为零
    NSDateComponents *components = [[NSDateComponents alloc] init];
    
    // 计算小时、分钟和秒
    int seconds = timeInterval;
    components.second = seconds % 60;
    int minutes = seconds / 60;
    components.minute = minutes % 60;
    components.hour = minutes / 60;
    
    return components;
}





+(NSString*)getCurrentTimes{

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制

    [formatter setDateFormat:@"YYYY-MM-dd"];

    //现在时间,你可以输出来看下是什么格式

    NSDate *datenow = [NSDate date];

    //----------将nsdate按formatter格式转成nsstring

    NSString *currentTimeString = [formatter stringFromDate:datenow];

    return currentTimeString;

}


@end
